<?php
//Loader::loadClass('AntiSpam', "modules/icarrent/pnincludes");
class PNTopics extends PNObjectEx {
  function PNTopics($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'icarrent_topics';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';

    //permission for post new | edit topic
    $func = FormUtil::getPassedValue ('func', false);
    $ctrl = FormUtil::getPassedValue ('ctrl', false);
    $this->_objPermission = array('component' => "icarrent:$func:$ctrl",
                                  'instance'  => '.*',
                                  'level'     => USER_EDIT_LEVEL);
    $this->_init($init, $where);
  }

/*  function validate(){
    $form = FormUtil::getPassedValue ('form', false);
    $is_validate = true;
    if (empty($form['title'])){
      $error = "ขออภัย ประกาศไม่มีหัวข้อกรุณาใส่หัวข้อ";
      SessionUtil::setVar('YLERROR', $error, '/', true, true);
      $is_validate = false;
    }



    if (iconv_strlen($form['detail'],"UTF-8") <= 10 ){
      $error = "ขออภัย รายละเอียดของประกาศสั้นเกินไป";
      SessionUtil::setVar('YLERROR', $error, '/', true, true);
      $is_validate = false;
    }


    //Valiadate image type & extendsion
    $images = $_FILES['images'];
    if(isset($images)){
      foreach ($images["error"] as $key => $error){
        $tmp_name = $images["tmp_name"][$key];
        $fliename = $images["name"][$key];
        $filetype = $images["type"][$key];
        $filesize = $images["size"][$key];

        $filename = stripslashes($fliename);
        $i = strrpos($filename,".");
        //if (!$i) { return ""; }
        $l = strlen($filename) - $i;
        $ext = substr($filename,$i+1,$l);
        $extension = strtolower($ext);
        
        $validate = 0;
        //image/jpeg
        if($extension == "jpeg" || $extension == "jpg" || $extension == "png" || ($filesize == 0 && $filename == "")){
          if (($filetype == "image/jpg")|| 
              ($filetype == "image/pjpeg")|| 
              ($filetype == "image/jpeg")|| 
              ($filetype == "image/png")
            ) {
              if ($error > 0) {
                $error = "ขออภัย  truck2hand  อนุญาติให้อัพโหลดไฟล์ .jpg, .jpeg และ .png";
                SessionUtil::setVar('YLERROR', $error, '/', true, true);
                $is_validate = false;
              }else{
                $is_validate = true;
              }
          }else if($filetype == ""){
            $is_validate = true;
         }else{
              $error = "ขออภัย  truck2hand  อนุญาติให้อัพโหลดไฟล์ .jpg, .jpeg และ .png";
              SessionUtil::setVar('YLERROR', $error, '/', true, true);
              $is_validate = false;
          }
        }else{
          $ctrl =  FormUtil::getPassedValue ('ctrl', null);              
          $error = "ขออภัย truck2hand อนุญาติให้อัพรูปประเภท jpg, jpeg และ png เท่านั้น";
          SessionUtil::setVar('YLERROR', $error, '/', true, true);
          $is_validate = false;
          return $is_validate;
        }
      }
    }

          $is_validate = true;
          return $is_validate;
  }
*/
  function updatePreProcess(){
    //check to delete icon or model or video
    $form = FormUtil::getPassedValue ('form', false );
    $id = $this->objData['id'];
    $this->_objData['ipaddress'] = InnoUtil::getIpAddress();

    //$this->_objData['lu_date'] = date("Y-m-d H:i:s");

    $this->checkColor();
  }

  function insertPreProcess(){
    //get ip address
    $this->_objData['ipaddress'] = InnoUtil::getIpAddress();
    $this->checkColor();
  }

  function insertPostProcess(){

    $form = FormUtil::getPassedValue ('form', false );

    //var_dump($form);exit;
    
    //Insert image
    Loader::loadClass('DataUtilEx', "modules/icarrent/pnincludes");
    $id = DBUtil::getInsertID ($this->_objType, $this->_objField);
    if ($id){
      //set ref_id
      //get category prefix
      $cate = DBUtil::selectFieldByID('icarrent_category','prefix',$this->_objData['category_id']);
      $subcate = DataUtilEx::fillString($this->_objData['subcategory_id'],2,'0');
      $ref_id = DataUtilEx::fillString($id,5,'0', $cate . $subcate);

      $cr_date = $this->_objData['cr_date'];

      $carproperty_id = $this->_objData['carproperty_id'];
      if($carproperty_id){
        $object = array('id'=>$id,'ref_id'=>$ref_id, 'carproperty_id'=>serialize($carproperty_id),'temp_cr_date'=>$cr_date);
      }else{
        $object = array('id'=>$id,'ref_id'=>$ref_id, 'temp_cr_date'=>$cr_date);
      }   
      
         
      //$object = array('id'=>$id,'ref_id'=>$ref_id, 'temp_cr_date'=>$cr_date);      
      DBUtil::updateObject($object,$this->_objType);

      $imagePath = $this->uploadFiles($id);
      $this->checkHasImage($id);

      ///////////////////////////////////////////////////////////////
      //Insert amount topic
      ///////////////////////////////////////////////////////////////
      $id      = $this->_objData['id'];
      $uid     = $this->_objData['cr_uid'];
      $title   = $this->_objData['title'];
      $price   = $this->_objData['price'];
      $type_id = $this->_objData['type_id'];

      $columnArray = array('amount_topic');
      $amount_topic = DBUtil::selectObject('icarrent_amount_topic' ,
                                           "WHERE amo_uid = '$uid'",
                                           $columnArray
                      );

      //var_dump( $amount_topic['amount_topic']+1);exit;
      if(isset($amount_topic) || $amount>0){
        $object = array(
                        'amount_topic' => $amount_topic['amount_topic']+1,
                        'last_topic_id' => $id,
                        'last_topic_type_id' => $type_id,
                        'last_topic_title' => $title,
                        'last_topic_price' => $price,
                        'first_thumb' => $imagePath['thumb'],
                        'first_image' => $imagePath['large']
                  );
        //var_dump($temp_amount_topic);
        //Do the insert
        $where = " amo_uid = ".$uid;
        DBUtil::updateObject($object, 'icarrent_amount_topic', $where);
        unset($object);
        //var_dump($temp_amount_topic); exit;
      }else{
        $object = array(
                        'uid' => $uid,
                        'amount_topic' => 1,
                        'last_topic_id' => $id,
                        'last_topic_type_id' => $type_id,
                        'last_topic_title' => $title,
                        'last_topic_price' => $price,
                        'first_thumb' => $imagePath['thumb'],
                        'first_image' => $imagePath['large']
                  );

        //Do the insert
        DBUtil::insertObject($object, 'icarrent_amount_topic');
        unset($object);
        //var_dump($amount_topic); exit;
      }
    }
  }

  function updatePostProcess(){
    $id = $this->_objData['id'];
    
    
    $carproperty_id = $this->_objData['carproperty_id'];
    //print_r($this->_objData); exit;    
    //set lastupdate
    if($id&$carproperty_id ){
      $lastupdate = date("Y-m-d");
      //$object = array('lastupdate'=>$lastupdate, 'subcategory_id'=>$form['subcategory_id'], 'carproperty_id'=>serialize($carproperty_id));
      $object = array('lastupdate'=>$lastupdate, 'carproperty_id'=>serialize($carproperty_id));
      //$object = array('id'=>$id,'ref_id'=>$ref_id, 'carproperty_id'=>serialize($carproperty_id),'temp_cr_date'=>$cr_date);      
      $where  = " tpc_id = ".$id;
      //print_r($object); exit;
      DBUtil::updateObject($object,'icarrent_topics',$where);
    }
    
    return true;
  }

/*
  function updatePostProcess(){
    //set thumb to image status
    $form = FormUtil::getPassedValue ('form', false );

    //var_dump($form);exit;
    $id = $this->_objData['id'];
    $carproperty_id = $this->_objData['carproperty_id'];
        
    //set lastupdate
    if($id&$carproperty_id ){
      $lastupdate = date("Y-m-d");
      //$object = array('lastupdate'=>$lastupdate, 'subcategory_id'=>$form['subcategory_id'], 'carproperty_id'=>serialize($carproperty_id));
      $object = array('lastupdate'=>$lastupdate, 'carproperty_id'=>serialize($carproperty_id));
      //$object = array('id'=>$id,'ref_id'=>$ref_id, 'carproperty_id'=>serialize($carproperty_id),'temp_cr_date'=>$cr_date);      
      $where  = " tpc_id = ".$id;
      print_r($object); exit;
      DBUtil::updateObject($object,'icarrent_topics',$where);
    }

    //set image thumb
    $thumb_id = $form['image_thumb'];
    if ($thumb_id && $id){
      //clear status
      $object = array('status' => 0);
      $where = "WHERE rsi_path = 'topics' AND rsi_referer_id = '$id'";
      DBUtil::updateObject($object,'icarrent_resource_image',$where);

      //set thumb
      $object = array('id' => $thumb_id , 'status' => 1);
      DBUtil::updateObject($object,'icarrent_resource_image');
      //Set first image to topics table
      $columnArray = array('filename','thumbname');
      $firstimage = DBUtil::selectObject('icarrent_resource_image' ,
                                              "WHERE rsi_referer_id = '$id' AND rsi_path = 'topics' AND rsi_status = 1",
                                              $columnArray
                                            );

      $object = array('firstimage'=>$firstimage[filename] , 'firsthumbimage'=>$firstimage[thumbname]);
      $where  = " tpc_id = ".$id;
      if ($id && $firstimage[filename]){
        DBUtil::updateObject($object,'icarrent_topics',$where);
      }
    }

    //delete topics image
    //Topics path

    //$images_topics_path = "resource/icarrent_upload/large/".$id."/topics/";
    //$thumbs_topics_path = "resource/icarrent_upload/medium/".$id."/topics/";


      $fakeId = ($id-45100);
      if($fakeId % 10000 == 0){
        $newNumberOfDirectory = ($fakeId/10000);
        $rootImagePath = "resource/icarrent_upload/large".$newNumberOfDirectory."/".$id."/topics/";
        $rootThumbPath = "resource/icarrent_upload/medium".$newNumberOfDirectory."/".$id."/topics/";
      }else if($id < 55100){
        $rootImagePath = "resource/icarrent_upload/large/".$id."/topics/";
        $rootThumbPath = "resource/icarrent_upload/medium/".$id."/topics/";
      }else{
        $rootImagePath = "resource/icarrent_upload/large".floor($fakeId/10000)."/".$id."/topics/";
        $rootThumbPath = "resource/icarrent_upload/medium".floor($fakeId/10000)."/".$id."/topics/";
      }



    foreach($form['image_delete'] as $delete_id){
      //Delete file from directory
      $columnArray = array('filename','thumbname');
      $objectImageName = DBUtil::selectObject('icarrent_resource_image',
                                              "WHERE rsi_path = 'topics' AND rsi_id = '$delete_id)'",
                                              $columnArray);
      unlink($images_topics_path.$objectImageName['filename']);
      unlink($thumbs_topics_path.$objectImageName['filename']);

      DBUtil::deleteObjectByID('icarrent_resource_image',$delete_id);
    }
    $image = $this->uploadFiles($id);
    $this->checkHasImage($id);



    ///////////////////////////////////////////////////////////////
    //Insert amount topic
    ///////////////////////////////////////////////////////////////
    $id      = $this->_objData['id'];
    $title   = $this->_objData['title'];
    $price   = $this->_objData['price'];
    $type_id = $this->_objData['type_id'];

    $userArray = array('cr_uid');
    $uid = DBUtil::selectObject('icarrent_topics' ,
                                         "WHERE tpc_id = '$id'",
                                         $userArray
                    );
    //var_dump($uid);
    //var_dump($this->_objData['cr_uid']);

    //var_dump($this->_objData); exit;

    $maxTopic = DBUtil::selectFieldMax('icarrent_topics' ,
                                       'id',
                                       'MAX',
                                       "WHERE tpc_cr_uid = '".$uid['cr_uid']."'"
                );
    //var_dump($maxTopic); exit;

    if ($thumb_id && $id && ($maxTopic == $id)){
      $object = array(
                      'last_topic_type_id' => $type_id,
                      'last_topic_title'   => $title,
                      'last_topic_price'   => $price,
                      'first_thumb'        => $rootThumbPath.$firstimage['filename'],
                      'first_image'        => $rootImagePath.$firstimage['filename']
                );
      //var_dump($temp_amount_topic);
      //Do the insert
      $where = " amo_uid = ".$uid['cr_uid'];
      DBUtil::updateObject($object, 'icarrent_amount_topic', $where);
      unset($object);
    }else if($maxTopic == $id){
      $object = array(
                      'last_topic_type_id' => $type_id,
                      'last_topic_title'   => $title,
                      'last_topic_price'   => $price
                );
      //var_dump($temp_amount_topic);
      //Do the insert
      $where = " amo_uid = ".$uid['cr_uid'];
      DBUtil::updateObject($object, 'icarrent_amount_topic', $where);
      unset($object);
    }
  }


*/


  function deletePreProcess(){

    $id = $this->_objData['id'];

    //Topics path
    //$images_topics_path = "resource/icarrent_upload/large/".$id;
    //$thumbs_topics_path = "resource/icarrent_upload/medium/".$id;


    $fakeId = ($id-45100);
    if($fakeId % 10000 == 0){
      $newNumberOfDirectory = ($fakeId/10000);
      $images_topics_path = "resource/icarrent_upload/large".$newNumberOfDirectory."/".$id;
      $thumbs_topics_path = "resource/icarrent_upload/medium".$newNumberOfDirectory."/".$id;
    }else if($id < 55100){
      $images_topics_path = "resource/icarrent_upload/large/".$id;
      $thumbs_topics_path = "resource/icarrent_upload/medium/".$id;
    }else{
      $images_topics_path = "resource/icarrent_upload/large".floor($fakeId/10000)."/".$id;
      $thumbs_topics_path = "resource/icarrent_upload/medium".floor($fakeId/10000)."/".$id;
    }


    if($id){
      $this->deleteDirectory($images_topics_path);
      $this->deleteDirectory($thumbs_topics_path);

      //delete topic
      DBUtil::deleteWhere('icarrent_topics',"WHERE tpc_id = '$id'");

      //delete post
      DBUtil::deleteWhere('icarrent_posts',"WHERE pst_topics_id = '$id'");
      DBUtil::deleteWhere('icarrent_resource_image',"WHERE rsi_referer_id = '$id' AND rsi_path = 'posts'");

      //delete image
      DBUtil::deleteWhere('icarrent_resource_image',"WHERE rsi_referer_id = '$id' AND rsi_path = 'topics'");
    }
  }
  

  function deletePostProcess(){



      $uid = SessionUtil::getVar('uid');
      $id = FormUtil::getPassedValue ('id', false );

      /////////////////////////////////////////////////////////////////
      //Query :: amount topic by user id
      /////////////////////////////////////////////////////////////////
      $amountArray = array('amount_topic');
      $amount_topic = DBUtil::selectObject('icarrent_amount_topic' ,
                                           "WHERE amo_uid = '$uid'",
                                           $amountArray
                      );
      //var_dump($amount_topic); exit;

      /////////////////////////////////////////////////////////////////
      //Query :: last topic by user id
      /////////////////////////////////////////////////////////////////
      $maxTopic = DBUtil::selectFieldMax('icarrent_topics' ,
                                         'id',
                                         'MAX',
                                         "WHERE tpc_cr_uid = '$uid'"
                  );
      //var_dump($maxTopic); exit;

      /////////////////////////////////////////////////////////////////
      //Query :: firstimage by topic id
      /////////////////////////////////////////////////////////////////
      $fakeId = ($maxTopic-45100);
      if($fakeId % 10000 == 0){
        $newNumberOfDirectory = ($fakeId/10000);
        $images_topics_path = "resource/icarrent_upload/large".$newNumberOfDirectory."/".$maxTopic."/topics/";
        $thumbs_topics_path = "resource/icarrent_upload/medium".$newNumberOfDirectory."/".$maxTopic."/topics/";
      }else if($maxTopic < 55100){
        $images_topics_path = "resource/icarrent_upload/large/".$maxTopic."/topics/";
        $thumbs_topics_path = "resource/icarrent_upload/medium/".$maxTopic."/topics/";
      }else{
        $images_topics_path = "resource/icarrent_upload/large".floor($fakeId/10000)."/".$maxTopic."/topics/";
        $thumbs_topics_path = "resource/icarrent_upload/medium".floor($fakeId/10000)."/".$maxTopic."/topics/";
      }

      $firstImageArray = array('type_id', 'title', 'price', 'firsthumbimage', 'firstimage', 'cr_uid');
      $topicArray = DBUtil::selectObject('icarrent_topics' ,
                                     "WHERE tpc_id = '$maxTopic'",
                                     $firstImageArray
                    );
      //var_dump($image); exit;

      /////////////////////////////////////////////////////////////////
      //Decrease amount topic && insert last topic && insert firstimage
      /////////////////////////////////////////////////////////////////
      $object = array(
                      'amount_topic'  => $amount_topic['amount_topic']-1,
                      'last_topic_id' => $maxTopic,
                      'last_topic_type_id' => $topicArray['type_id'],
                      'last_topic_title'   => $topicArray['title'],
                      'last_topic_price'   => $topicArray['price'],
                      'first_thumb'        => $thumbs_topics_path.$topicArray['firsthumbimage'],
                      'first_image'        => $images_topics_path.$topicArray['firstimage']
                );

      //var_dump($temp_amount_topic);
      //Do insert
      //$where = " amo_uid = ".$topicArray['cr_uid'];
      $where = " amo_uid = ".$uid;
      DBUtil::updateObject($object, 'icarrent_amount_topic', $where);
      unset($object);

  }

  function deleteDirectory($dirname) {
    if (is_dir($dirname))
       $dir_handle = opendir($dirname);
    if (!$dir_handle)
       return false;
    while($file = readdir($dir_handle)) {
       if ($file != "." && $file != "..") {
          if (!is_dir($dirname."/".$file))
             unlink($dirname."/".$file);
          else
             $this->deleteDirectory($dirname.'/'.$file);    
       }
    }
    closedir($dir_handle);
    rmdir($dirname);
    return true;
  }


 

  function checkColor(){
    if ($this->_objData['bgcolor'] == '#000000'){
      $this->_objData['bgcolor'] = '';
    }
  }

  function uploadFiles($id){
    $count = DBUtil::selectObjectCount('icarrent_resource_image' , "WHERE rsi_referer_id = '$id' AND rsi_path = 'topics'");

    if ($id && in_array(0,$_FILES['images']['error'])){
      $images = $_FILES['images'];

      //Set directory to store image path

      $fakeId = ($id-45100);
      if($fakeId % 10000 == 0){
        $newNumberOfDirectory = ($fakeId/10000);
        $rootImagePath = "resource/icarrent_upload/large".$newNumberOfDirectory."/";
        $rootThumbPath = "resource/icarrent_upload/medium".$newNumberOfDirectory."/";
      }else if($id < 55100){
        $rootImagePath = "resource/icarrent_upload/large/";
        $rootThumbPath = "resource/icarrent_upload/medium/";
      }else{
        $rootImagePath = "resource/icarrent_upload/large".floor($fakeId/10000)."/";
        $rootThumbPath = "resource/icarrent_upload/medium".floor($fakeId/10000)."/";
      }

      //Make root topic image directory 
      if (!is_dir($rootImagePath)) {
        mkdir($rootImagePath, 0755);
      }
      //Make root topic thumb directory
      if (!is_dir($rootThumbPath)) {
        mkdir($rootThumbPath, 0755);
      }

      //$imagespathbyid = $rootImagePath."/".$id."/";
      //$thumbspathbyid = $rootThumbPath."/".$id."/";
      $imagespathbyid = $rootImagePath.$id."/";
      $thumbspathbyid = $rootThumbPath.$id."/";

      //Make topic image directory by id
      if (!is_dir($imagespathbyid)) {
        mkdir($imagespathbyid, 0755);
      }
      //Make topic thumb directory by id
      if (!is_dir($thumbspathbyid)) {
        mkdir($thumbspathbyid, 0755);
      }

      //Set directory to store image path by id
      $imagespath = $imagespathbyid."topics/";
      $thumbspath = $thumbspathbyid."topics/";
      //Make topic image directory by id
      if (!is_dir($imagespath)) {
        mkdir($imagespath, 0755);
      }
      //Make topic thumb directory by id
      if (!is_dir($thumbspath)) {
        mkdir($thumbspath, 0755);
      }

      foreach ($images["error"] as $key => $error){
        if ($error == 0){
            //Gennerate next id
            $image_id = DBUtil::selectFieldMax( 'icarrent_resource_image', 'id', 'MAX', '');
            if($image_id == null){
              $next_id = 1;
            }else{
              $next_id = $image_id+1;
            }

            $tmp_name = $images["tmp_name"][$key];
            $fliename = $images["name"][$key];
            $filetype = $images["type"][$key];
            $filesize = $images["size"][$key];

////////////////////////// get the extension of the file in a lower case format/////////////////////////////////////////////
            $filename = stripslashes($fliename);
            $i = strrpos($filename,".");
            if (!$i) { return ""; }
            $l = strlen($filename) - $i;
            $ext = substr($filename,$i+1,$l);
            $extension = strtolower($ext);

////////////////////////////generate new file&path/////////////////////////////////////////////////////////////////////
            $rootname       = $next_id.time().".".$extension;
            $filename_temp  = "images_".$id.$rootname;
            $thumbname_temp = "thumbs_".$id.$rootname;
            $imgpath        = $imagespath.$filename_temp;
            $tmbpath        = $thumbspath.$filename_temp;

            if($key==0){
              $firstimage_temp = $filename_temp;
            }

//////////////////////////start upload/////////////////////////////////////////////

        $source = $tmp_name;
        $target = $imgpath;
        move_uploaded_file($source, $target);

        //images
        $imagepath = $fliename;
        $save = $imgpath; //This is the new file you saving
        $file = $imgpath; //This is the original file
        list($width, $height) = getimagesize($file) ; 

//////////////////resize image orginal////////////////////
        $modwidth = 600; 
        $diff = $width / $modwidth;
        if (($width > 600) || ($height > 200)) 
        {
          $modheight = $height / $diff; 
          $tn = imagecreatetruecolor($modwidth, $modheight); 
          if($filetype == "image/pjpeg" || $filetype == "image/jpeg"){
            $image = imagecreatefromjpeg($file) ; 
          }elseif($filetype == "image/x-png" || $filetype == "image/png"){
            $image = imagecreatefrompng($file) ;
          }elseif($filetype == "image/gif"){
            $image = imagecreatefromgif($file) ;
          }
            imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
        }

        if($filetype == "image/pjpeg" || $filetype == "image/jpeg"){
          imagejpeg($tn, $save, 100) ; 
        }elseif($filetype == "image/x-png" || $filetype == "image/png"){
          imagepng($tn, $save) ; 
        }elseif($filetype == "image/gif"){
          copy($file, $save) ; 
        }

//////////////////resize image thumb////////////////////

        $save = $tmbpath; //This is the new file you saving
        $file = $imgpath; //This is the original file

        list($width, $height) = getimagesize($file) ; 

        $modwidth = 100; 
        $diff = $width / $modwidth;
        $modheight = $height / $diff; 
        $tn = imagecreatetruecolor($modwidth, $modheight); 
        if($filetype == "image/pjpeg" || $filetype == "image/jpeg"){
          $image = imagecreatefromjpeg($file) ; 
        }elseif($filetype == "image/x-png" || $filetype == "image/png"){
          $image = imagecreatefrompng($file) ;
        }elseif($filetype == "image/gif"){
          $image = imagecreatefromgif($file) ;
        }
        imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

        if($filetype == "image/pjpeg" || $filetype == "image/jpeg"){
          imagejpeg($tn, $save, 100) ; 
        }elseif($filetype == "image/x-png" || $filetype == "image/png"){
          imagepng($tn, $save) ; 
        }elseif($filetype == "image/gif"){
          copy($file, $save) ; 
        }


            $objects = array(
                            'id'         => $next_id,
                            'path'       => 'topics',
                            'referer_id' => $id,
                            'filename'   => $filename_temp,
                            'filesize'   => $filesize,
                            'filetype'   => $filetype,
                            'thumbname'  => $filename_temp,
                            'thumbsize'  => $thumbsize,
                            'thumbtype'  => $thumbtype
                           );

          DBUtil::insertObject($objects,'icarrent_resource_image');
        }
      }
      //Set default images
      if ($id && $count==0){
        $object = array('firstimage'=>$firstimage_temp , 'firsthumbimage'=>$firstimage_temp);
        $where  = " tpc_id = ".$id;
        DBUtil::updateObject($object,'icarrent_topics',$where);
      }
      $returnImagePath['thumb'] = $thumbspath.$firstimage_temp;
      $returnImagePath['large'] = $imagespath.$firstimage_temp;

      return $returnImagePath;
    }
  }

  function checkHasImage($id){
    $count = DBUtil::selectObjectCount('icarrent_resource_image' , "WHERE rsi_referer_id = '$id' AND rsi_path = 'topics'");
    if ($count && $id){
      $object = array('id'=>$id , 'has_image'=>$count);
      DBUtil::updateObject($object,$this->_objType);
    }else{
      $object = array('id'=>$id , 'has_image'=>0);
      DBUtil::updateObject($object,$this->_objType);
    }
  }

  //

}